window.scenario = {
		"longCycles" : [[20,30,35,40],[30,30,35,40],[20,35,35,40], [22,30,35,40], [16,25,30,40]],
		"shortCycles" : [[15,25,30,40],[25,25,35,40],[15,30,30,40]],
		"foodStationCycle" : [10,15,20,10, 20, 15, 10, 10],
		"meteorIntensisy" : [10, 15, 10, 0, 10, 5, 10, 1],
		"scenarioName" : "scenariowithoutWarning",
		"gameLength" : 360,
		"maxFoodInventory" : 40,
		"fs_actionCost" : 50,
		"fs_actionTime" : 3,
		"fs_spaceshipCost" : 200,
		"fs_spaceshipTime" : 3,
		"fs_spaceshipDelayBeforeFlight" : 7,
		
		"mc_maintenanceAddTime" : 17,
		
		"mc_maintenanceActionTime" : 3,
		"mc_maintenanceActionCost" : 50,
		"mc_properActionTime" : 0,
		"mc_properActionCost" : 0,
		"mc_minorActionTime" : 10,
		"mc_minorActionCost" : 50,
		"mc_moderateActionTime" : 10,
		"mc_moderateActionCost" : 150,
		"mc_severeActionTime" : 10,
		"mc_severeActionCost" : 200,
		"mc_fireRate" : [15,15,20,50],
		"mc_spaceshipSpeed" : [20,20,20,10],
		"speedChangeSever" : 0.75,
		"shootingBreakPercentSever" : 50,
		"shootingBreakPercentModerate" : 20,
		"meteorScore" : 10,
		getActionTime : function (mc) {
			switch (mc) {
			case 1:
				//console.error("player shouldn't take repair action when mc==1 (proper)");
				return this.mc_properActionTime;
			case 2:
				return this.mc_minorActionTime;
			case 3:
				return this.mc_moderateActionTime;
			case 4:
				return this.mc_severeActionTime;

			}
		},
		getActionCost : function (mc) {
			switch (mc) {
			case 1:
				//console.error("player shouldn't take repair action when mc==1 (proper)");
				return this.mc_properActionCost;
			case 2:
				return this.mc_minorActionCost;
			case 3:
				return this.mc_moderateActionCost;
			case 4:
				return this.mc_severeActionCost;

			}
		},
		
		getFoodFillTime : function () {
			return this.fs_actionTime;
		},
		
		getFoodFillCost : function () {
			return this.fs_actionCost;
		},
		
		getShootingBreakPercent : function (mc) {
			if (mc >= 4){
				return this.mc_fireRate[3];
			}
			return this.mc_fireRate[mc];
		},
		
		getSpeed : function (mc) {
			if (mc >= 4){
				return this.mc_spaceshipSpeed[3];
			}
			return this.mc_spaceshipSpeed[mc];
		}

};


window.messages = {
		"repairAction" 					: {"type" : 1, "name" : "repair action"},
		"maintenaneAction" 				: {"type" : 2, "name" : "maintenane action"},
		"maintenaneTime" 				: {"type" : 3, "name" : "maintenane time"},
		"foodFillDone" 					: {"type" : 4, "name" : "food fill"},
		"foodFillBySpaceShipDone" 		: {"type" : 5, "name" : "food fill by spaceship"},
		"foodStationCreated" 			: {"type" : 6, "name" : "food station created"},
		"mechanicalConditionChanged" 	: {"type" : 7, "name" : "mechanical condition changed"},
		"newCycleCreated" 				: {"type" : 8, "name" : "new cycle created"},
		"timeToMaintenance"				: {"type" : 9, "name" : "time to do maintenance"},
};

//Mechnical Condition parameters
var currentMC = 1;

var MC_PROPER = 1;
var MC_MINOR_MF = 2; // MF - malfunction
var MC_MODERATE_MF = 3; // MF - malfunction
var MC_SEVERE_MF = 4; // MF - malfunction
var MC_MAX_MF = 4;


var needDoMaintenance = new Boolean(false);
var maintenanceTime = calcMaintenanceTime(0);

var inRepair = 0;
var mc_cycleIndex = 0;
var currentCycle = createNewMcCycle(needDoMaintenance, 0);


function processMechanicalCondition(time) { // todo: implement sub method
	if (needDoMaintenance == false && time >= maintenanceTime){
		needDoMaintenance = true;
	}

	newMc = calcNewMechanicalCondition(time);
	if (newMc > currentMC){
		//console.log("mc change to " + newMc);
		writeStatisticMsg(messages.mechanicalConditionChanged, "from " + currentMC + " to " + newMc);
		currentMC = newMc;
		changeMechanicalCondition(currentMC);
		setShootingBreakPercent(scenario.getShootingBreakPercent(currentMC));
		game.ship.setSpeed(scenario.getSpeed(currentMC));
	}

	//mc5 = setMechanicalCondition(time);

	
//	warning strategy will be implemented later
}


function calcMaintenanceTime(time){
	//writeStatisticMsg(messages.maintenaneTime, "set time to" + (time + scenario.mc_maintenanceAddTime) + ",");
	return time + scenario.mc_maintenanceAddTime;
}
//called when player click on mechanical action button.
function mechanicalActionDone() {
	writeStatisticMsg(messages.repairAction, "mc = " + currentMC + ",");
	if (currentMC != MC_PROPER){
		var actionTime = scenario.getActionTime(currentMC);
		var actionCost = scenario.getActionCost(currentMC);
		currentCycle = createNewMcCycle(needDoMaintenance, currentTime + scenario.getActionTime(currentMC));
		
		currentMC = MC_PROPER;
		changeMechanicalCondition(MC_PROPER);
		setShootingBreakPercent(scenario.getShootingBreakPercent(currentMC));
		game.ship.setSpeed(game.ship.speed * scenario.getSpeed(currentMC));

		increaseScore(-actionCost);
		freezeSpaceship(actionTime);
		
	}
}

function maintenanceActionDone() {
	needDoMaintenance = false;
	maintenanceTime = calcMaintenanceTime(currentTime);

	increaseScore(-scenario.mc_maintenanceActionCost);
	freezeSpaceship(scenario.mc_maintenanceActionTime);
	

	writeStatisticMsg(messages.maintenaneAction, "mc = " + currentMC + ", nextMaintenanceTime = " + maintenanceTime);
	if (currentMC == MC_PROPER){
		currentCycle = createNewMcCycle(needDoMaintenance, currentTime + scenario.mc_maintenanceActionTime);
	}
}

function createNewMcCycle(needDoMaintenance, shiftedTime){
	if (needDoMaintenance == true){
		var offsetCycle = scenario.shortCycles[mc_cycleIndex].slice();
	} else {
		offsetCycle = scenario.longCycles[mc_cycleIndex].slice();
	}
	for (var i = 0; i < offsetCycle.length; i++) {
		offsetCycle[i] = offsetCycle[i] + shiftedTime;
	}
	writeStatisticMsg(messages.newCycleCreated, "mc_cycleIndex = " + mc_cycleIndex + ", needDoMaintenance = " + needDoMaintenance + ", cycle = " + offsetCycle.join());
	mc_cycleIndex += 1;
	if (mc_cycleIndex >= scenario.longCycles.length){
		mc_cycleIndex = 0;
	}

	return offsetCycle;
}

function calcNewMechanicalCondition(time){
	if (currentCycle == undefined){
		currentCycle = createNewMcCycle(needDoMaintenance, time);
		return currentMC;
	}

	if (currentMC == MC_MAX_MF){
		return MC_MAX_MF;
	}

	if (time > currentCycle[currentMC-1]){
		return currentMC + 1;
	}
	return currentMC;
}

//Food Satation parameters
var lastTimeFoodStationTime = 0;
var fs_index = 0;
var fs_nextFoodStationTime = scenario.foodStationCycle[0];



//Called when spaceship stop in food station.
function foodFillDone() {
	writeStatisticMsg(messages.foodFillDone, "inventory = " + game.foodStatus);
	setFoodInventory(scenario.maxFoodInventory);
	increaseScore(-scenario.getFoodFillCost());
	freezeSpaceship(scenario.getFoodFillTime());
}

function foodFillBySpaceship(){
	setFoodInventory(scenario.maxFoodInventory);
	increaseScore(-scenario.fs_spaceshipCost);
	freezeSpaceship(scenario.fs_spaceshipTime);
	
}

function processFoodInventory(time) {

	if (game.foodStatus <= 0) {
		//seen that total time of fill food by spaceship is: (delay before time + flight time + fill time)
		callToFoodSpaceship(scenario.fs_spaceshipDelayBeforeFlight);
	}
	calcNextStation(time);
	// warning strategy will be implemented later
}



function calcNextStation(time){
	if (fs_nextFoodStationTime == undefined){
		fs_nextFoodStationTime = scenario.foodStationCycle[0] + time;
	}
	if (time > fs_nextFoodStationTime){
		writeStatisticMsg(messages.foodStationCreated, "");
		var x = Math.floor(Math.random() * (Background.prototype.canvasWidth - 2 * imageRepository.foodship.width - 20) / imageRepository.foodship.width) * imageRepository.foodship.width + imageRepository.foodship.width + 10;
		makeAnewFoodStation(x);
		if (fs_index == undefined){
			fs_index = 0;
		}
		fs_index++;
		if (fs_index >= scenario.foodStationCycle.length){
			fs_index = 0;
		}
		fs_nextFoodStationTime = time + scenario.foodStationCycle[fs_index];
	}
}

function controllerLoop(time) {
	processMechanicalCondition(time);
	processFoodInventory(time);
	//processMeteorsIntensity(time);
	//to make a foodstation


	//if (time-lastTimeFoodStationTime >= 15) {
//	var x = Math.floor(Math.random() * (Background.prototype.canvasWidth - 2 * imageRepository.foodship.width - 20) / imageRepository.foodship.width) * imageRepository.foodship.width + imageRepository.foodship.width + 10;
//	makeAnewFoodStation(x);
//	lastTimeFoodStationTime= time;
//	}
}

function setMeteorNum(number)
{
	game.enemyBulletPool.setSize(number);

	//game.enemyBulletPool.init("enemyBullet");
	//game.enemyBulletPool.getPool();
}

var intensityIndex = 0;
var nextChangeIntensitiyTime = scenario.meteorIntensisy[0];

function processMeteorsIntensity(time){
	if (time > nextChangeIntensitiyTime){
		intensityIndex = intensityIndex +2;
		if (intensityIndex >= scenario.meteorIntensisy.length){
			intensityIndex = 0;
		}
		setMeteorNum(scenario.meteorIntensisy[intensityIndex + 1]);
		nextChangeIntensitiyTime = time + scenario.meteorIntensisy[intensityIndex];
	}
}

function writeStatisticMsg(msgTitle, content)
{
	var data = {
			file: window.fileName,
			folder: window.folderName,
			score: game.playerScore,
			foodLevel: game.foodStatus,

			mechanicalCondition: currentMC,
			msgType: msgTitle.type,
			msgName: msgTitle.name,
			content: content};

	$.ajax({
		type: "POST",
		url: "http://localhost:22742/MGGameService.svc/recordData",
		//url: "http://biu-ai.com/MGgame/MGGameService.svc/recordData",
		data: JSON.stringify(data),
		dataType: 'json'
	});
}
